package com.pandora.framework.util;

import java.util.LinkedHashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.pandora.common.util.CommonUtils;
import com.pandora.framework.constant.Constants;
import com.pandora.framework.constant.ResponseConstants;
import com.pandora.framework.constant.ServiceConstants;

/**
 * 相应工具类
 * 
 * @author JingChenglong 2017/12/28 18:23
 *
 */
public class ResponseUtils {

	private static final String ROOT = "response";
	private static final String CONTENT = "content";

	private String format = ServiceConstants.XML;
	private String logNo = "";

	public static ResponseUtils getInstance(String format, String logNo) {
		return new ResponseUtils(format, logNo);
	}

	public ResponseUtils(String format, String logNo) {
		super();
		this.format = format;
		this.logNo = logNo;
	}

	public ResponseUtils() {
		super();
	}

	@SuppressWarnings("unchecked")
	public String response(Object resObject) {

		if (resObject == null) {
			return null;
		}

		if (resObject instanceof Integer) {
			return response((((Integer) resObject).intValue()));
		} else if (resObject instanceof String) {
			return response(resObject.toString());
		} else if (resObject instanceof Map) {
			return response((Map<String, Object>) resObject);
		} else {
			return null;
		}
	}

	private String response(int resObject) {

		return doResponse(getInfoMap(resObject));
	}

	private String response(String resObject) {

		return doResponse(getInfoMap(resObject));
	}

	private String response(Map<String, Object> content) {

		Map<String, Object> map = getInfoMap();
		map.put(CONTENT, content);

		return doResponse(map);
	}

	private String doResponse(Map<String, Object> map) {
		if (ServiceConstants.XML.equalsIgnoreCase(this.format)) {
			Document doc = DocumentHelper.createDocument();
			// TODO 使用dom4j将响应结果转化为XML格式
			return doc.asXML();
		} else if (ServiceConstants.JSON.equalsIgnoreCase(this.format)) {
			try {
				JSONObject json = new JSONObject();
				json.put(ROOT, map);
				return json.toString();
			} catch (Exception e) {
				LogUtils.logError(CommonUtils.getTraceInfo() + e.getMessage());
				return "";
			}
		} else {
			LogUtils.logError(CommonUtils.getTraceInfo() + ResponseConstants.REQUEST_CONTENT_FORMAT_ERROR);
			return "";
		}
	}

	public Map<String, Object> getInfoMap() {

		return getInfoMap(ResponseConstants.SUCCESS);
	}

	public Map<String, Object> getInfoMap(int code) {

		return getInfoMap(String.valueOf(code));
	}

	private Map<String, Object> getInfoMap(String code) {

		Map<String, Object> map = new LinkedHashMap<String, Object>();
		Map<String, Object> info = new LinkedHashMap<String, Object>();

		if (!ServiceUtils.isInfoCode(code)) {
			code = String.valueOf(ResponseConstants.SYSTEMBUSY);
		}

		String msg = PropertiesUtils.getCommonMessage(code);
		if (StringUtils.isEmpty(msg) || Constants.UNKNOWN.equals(msg)) {
			msg = PropertiesUtils.getBusinessMessage(code);
		}

		info.put("code", code);
		info.put("msg", msg);
		info.put("version", PropertiesUtils.getConfig("VERSION"));
		if (!String.valueOf(ResponseConstants.SUCCESS).equalsIgnoreCase(code)) {
			info.put("logno", logNo);
		}

		map.put("info", info);
		return map;
	}
}